//app.js
var netUtil = require('./utils/netUtil.js')
var util = require('./utils/util.js')
App({
	onLaunch: function() {

	},
	
	onShow(res) {

    },

	getUserOpedId: function(callback,arrParams,force) {
		console.log('getUserOpedId');
		if(typeof callback == "function") {
			console.log('callback is function');
		} else {
			console.log('callback not function');
		};
		var that = this
		if(force != undefined) {
			console.log('app.getUserOpedId 强制更新:' + force);
		}
		
		if(this.globalData.userInfo.openid == null || this.globalData.userInfo.openid == "" || force == true) {
			console.log('app.getUserOpedId 连网更新userid和sessinkey');
			wx.login({
				success: function(res) {
					console.log(res.code);
					var params = {
						method: 'wechat.wisen.login.oppenId.query',
						data : {
  							code: res.code
  						}						
					};

					netUtil.buildRequest(that, '/wisen/api/wechat/ret', params, {
						onPre: function() {
							netUtil.showLoadingDialog(that);
						},
						onSuccess: function(data) {
							console.log(data);
							netUtil.hideLoadingDialog(that);
							that.globalData.userInfo.openid = data.data.openid;

							if(typeof callback == "function") {
								console.log('call callback()');
								if(arrParams != undefined) {
									 callback(arrParams);
								} else {
									 callback();
								}
               				   
							} else {
								console.log(' callback not a function');
								wx.reLaunch({
									url: '/pages/index/index',
								});
							}
						},
						onError: function(msgCanShow, code, hiddenMsg) {
							netUtil.hideLoadingDialog(that);
							netUtil.showAlertDialog("提示", msgCanShow, false, "确定", null, null);
						},
					}).send();

				},
				fail: function(res) {
					netUtil.showConfirmModal('wx.login失败' + res.errMsg);
					console.log('wx.login失败:' + res.errMsg);
				}
			});
		} else {
			console.log('getUserOpedId 已有数据：' + this.globalData.userInfo.openid);
			if(typeof callback == "function") {
				console.log('call callback()');
				if(arrParams != undefined) {
					callback(arrParams);
				} else {
					callback();
				}
			} else {
				console.log(' callback not a function');
			}
		}
	},
	
	//检查用户是否已经注册
	checkHasRegister : function(type,callback) {
		var that = this
		this.getUserOpedId(function() {
			
			var params = {
				method: 'wechat.wisen.login.oppenId.check',
				openId : that.globalData.userInfo.openid,
				data : {
  					type: type
  				}	
			};
			
			netUtil.buildRequest(that,'/wisen/api/wechat/ret',params,{
				
				onPre: function() {
					netUtil.showLoadingDialog(that);
				},
				
				onSuccess: function(resp) {
					
					netUtil.hideLoadingDialog(that);
				
					if(resp.ret == that.globalData.respSuccessCode) {     //如果接口返回0.证明已经注册过，不做处理，正常执行
						if(callback != undefined && callback != null) {
							var phone = resp.data.phone;
							callback(phone);
						}
						
 					} else {
 						
 						var obj = null;
 						var data = resp.data;
 												
 						if(data != null && data != undefined) {
 							
 							if(data == "1" ) {   //跳转到注册页面
 								wx.reLaunch({
									url: '/pages/register/register'
								});
 							} else if (data == "2") {    //跳转到欠费页面
 								wx.navigateTo({
									url: '/pages/mypay/mypay?toDebt=yes'
								});
 							} else {
 								wx.reLaunch({
									url: '/pages/register/register'
								});
 							}
 							
 						} else {
 										
							if(resp.message != undefined && resp.message != null && resp.message != '') {
 								netUtil.showAlertDialog("提示", resp.message, false, "确定", null, function() {
 									wx.reLaunch({
										url: '/pages/index/index'
									});
 								});
 							} else {
 								wx.reLaunch({
									url: '/pages/register/register'
								});
 							}
 						}
 						
 						/**/
					
					}
					
				},
				
				onError: function(res) {
					console.log("fail");
					netUtil.hideLoadingDialog(that);
					netUtil.showAlertDialog("提示", msgCanShow, false, "确定", null, null);
				}
				
			}).send();
		});
		
		
	},		

	globalData: {
		userInfo: {
			openid : ''
		},
		
		//公众参数
		commonParams: {
			format: 'json',
			sign: 'WISEN_WECHAT'
		},
		
		respSuccessCode : 0,
		
		//http://test.topwms.cn:8083/cs/wisen/api/wechat/ret
		//url 地址前缀


		

		apiHeadUrl: 'http://test.topwms.cn:8084/cs', 
    //apiHeadUrl: 'http://localhost:8866/cs/',

	//apiHeadUrl: 'http://test.topwms.cn:8083/cs',

	
	//	apiHeadUrl: 'https://wms.topwms.cn/cs',
   //apiHeadUrl: 'http://test.topwms.cn:8083/cs',

	
  //  apiHeadUrl: 'http://test.topwms.cn:8083/cs/',
		//套餐选项配置表，统一从这里拿
		comboArray : [
		 		{
		 			comboKey:'A',
		 			comboValue:'月'
		 			
		 		},{
		 			comboKey:'B',
		 			comboValue:'季'
		 		},{
		 			comboKey:'C',
		 			comboValue:'年'
		 		}
		],
		 
		logisticsCompanyArray: [{
			logisticsCompany: '德邦物流'
		}, {
			logisticsCompany: '德邦快递'
		}, {
			logisticsCompany: '顺丰快递'
		}, {
			logisticsCompany: '中通快递'
		}, {
			logisticsCompany: '中通物流'
		}, {
			logisticsCompany: '安能物流'
		}, {
			logisticsCompany: '自提'
		}],
		
		saveTeaLogisticsCompanyArray: [{
			logisticsCompany: '德邦物流'
		}, {
			logisticsCompany: '德邦快递'
		}, {
			logisticsCompany: '顺丰快递'
		}, {
			logisticsCompany: '中通快递'
		}, {
			logisticsCompany: '中通物流'
		}, {
			logisticsCompany: '安能物流'
		}, {
			logisticsCompany: '自送'
		}]
	
	}
})